package com.briup.labs.controller;

import com.briup.labs.pojo.*;
import com.briup.labs.service.SmartLabsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/labs")
public class LabsController {

    @Autowired
    private SmartLabsService smartLabsService;

    @GetMapping("/all")
    public Result<List<SmartLabs>> all() {
        List<SmartLabs> list = smartLabsService.all();
        return Result.success(list);
    }

    @GetMapping("/by/condition")
    public Result<PageBean<Labs>> list(String pageNum,
                                       String pageSize,
                                       @RequestParam(required = false) String key) {
        //List<SmartLabs> smartLabs = smartLabsService.list(key);
        PageBean<Labs> pageBean
                = smartLabsService.list(pageNum, pageSize, key);
        if (pageBean.getTotal() != null && pageBean.getItems() != null) {
            List<SmartLabs> smartLabs = smartLabsService.list(key);
            pageBean.setTotal((long) smartLabs.size());
            return Result.success(pageBean);
        } else {
            return Result.error("输出传入格式错误");
        }
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SmartLabs smartLabs) {
        smartLabsService.saveOrUpdate(smartLabs);
        return Result.success();
    }

    @PostMapping("/saveOrUpdateLabsAndUser")
    public Result saveOrUpdateLabsAndUser(@RequestBody SmartLabsAdminR smartLabsAdminR) {
        smartLabsService.saveOrUpdateLabsAndUser(smartLabsAdminR);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        smartLabsService.delete(id);
        return Result.success();
    }
}
